<?php

namespace App\Exceptions\Custom;


use Exception;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Http;
use Symfony\Component\HttpFoundation\Response;

/**
 * @author greenLeaf
 */
class CustomHttpResponseException extends HttpResponseException
{


    /**
     * @param int|array $errCodeTrans
     * @param Exception|null $exception
     * @param string $msg
     * @param array $data
     * @param bool $logError
     * @param bool $fsNotify
     * @param int $httpStatusCode
     */
    public function __construct(
        $errCodeTrans,
        Exception $exception = null,
        $msg = "",
        array $data = [],
        bool $logError = true,
        bool $fsNotify = true,
        int $httpStatusCode = Response::HTTP_OK
    ) {
        if ($exception instanceof Exception && ($fsNotify || $logError)) {
            //记录日志｜飞书
            logException("程序运行报错 {$msg}", $exception, [], $logError, $fsNotify);
        }

        if (is_array($errCodeTrans)) {
            list($errCode, $codeMsg) = $errCodeTrans;
            if (empty($msg) && !empty($codeMsg)) {
                $msg = $codeMsg;
            }
        } else {
            $errCode = $errCodeTrans;
        }

        $res = [
            "code" => $errCode,
            "msg"  => $msg,
        ];
        if ($data) {
            $res['data'] = $data;
        }
        parent::__construct(response()->json($res, $httpStatusCode));
    }

}
